package com.chen.controller;

import com.chen.pojo.User;
import com.chen.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/blog")
public class UserController {

    //组合service
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    //用户登录login
    @RequestMapping("/login")
    public String login(String userName, String passWord, HttpServletRequest request, Model model){
        User user = userService.getUser(userName, passWord);
        if (user!=null){
            model.addAttribute("user",user);
            ServletContext context = request.getSession().getServletContext();
            context.setAttribute("user",userName);
            return "/admin/main";
        }
        else {
            return "/login";
        }
    }

    //用户退出
    @RequestMapping("/loginOut")
    public String loginOut(){
        Subject subject = SecurityUtils.getSubject();
        if (subject!=null){
            subject.logout();
        }
        return "/login";
    }

    //关于我们
    @RequestMapping("about")
    public String about(){

        return "/admin/about";
    }




}
